/**
 * 学工处老师模型
 */
package dms.model.role;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import dms.dto.StudentAffairTeacherDTO;
import dms.dto.StudentDTO;
import dms.model.Classes;
import dms.model.Dormitory;
import dms.model.Faculty;
import dms.model.School;
import dms.model.exception.NoPermissionException;
import dms.model.form.DormitoryChange;
import dms.repository.DormitoryChangeRepository;
import dms.repository.DormitoryRepository;
import dms.repository.SchoolRepository;
import dms.repository.StudentRepository;
import dms.repository.SystemAdministratorRepository;

/**
 * @author 李健
 *
 */
public class StudentAffairTeacher {

	private static final Log log = LogFactory.getLog(SystemAdministrator.class);	
	//实体数据
	private Account account;
	private Long id;
	private School school;
	/**
	 * @return the account
	 */
	public Account getAccount() {
		return account;
	}
	/**
	 * @param account the account to set
	 */
	public void setAccount(Account account) {
		this.account = account;
	}
	/**
	 * @return the id
	 */
	public Long getId() {
		return id;
	}
	/**
	 * @param id the id to set
	 */
	public void setId(Long id) {
		this.id = id;
	}
	/**
	 * @return the school
	 */
	public School getSchool() {
		return school;
	}
	/**
	 * @param school the school to set
	 */
	public void setSchool(School school) {
		this.school = school;
	}
	/* (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}
	/* (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		StudentAffairTeacher other = (StudentAffairTeacher) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}
	
	
	
	

	
	
	/*//学工处老师用到的仓储
	private StudentRepository studentRepository = null;
	private DormitoryRepository dormitoryRepository = null;
	private SystemAdministratorRepository systemAdministratorRepository = null;
	private SchoolRepository schoolRepository = null;
	private DormitoryChangeRepository dormitoryChangeRepository = null;*/
	/******************************************************************************************/
	/*// Domain operation 
	public Student addStudent(StudentDTO studentInfo) throws NoPermissionException {
		Classes classes = this.schoolRepository.findClassesById(studentInfo.getClasses());
		Faculty faculty = classes.getFaculty();
		School school = faculty.getSchool();
		
		//如果该老师增加的学生不是他学院的则不添加
		if (!school.getId().equals(this.getSchool().getId())) {
			throw new NoPermissionException("学生与老师学院不一致");
		}
		Student student = new Student();
		Account account = new Account(Role.STUDENT);
		Dormitory dormitory = this.dormitoryRepository.findById(studentInfo.getDormitory());
		
		try {
			BeanUtils.copyProperties(account, studentInfo);
		} catch (IllegalAccessException e) {
			
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			
			e.printStackTrace();
		}
		
		Date date = new Date();
		account.setRegisterDate(date);
		account.setModifyDate(date);
		account.setId(null);
		
		student.setClasses(classes);
		student.setDormitory(dormitory);
		student.setFaculty(faculty);
		student.setGender('M');
		student.setSchool(school);
		student.setStudentNumber(studentInfo.getStudentNumber());
		
		student.setId(null);
		student.setAccount(account);
		
		return this.studentRepository.createStudent(student);
	}
	
	public Student updateStudent(StudentDTO studentInfo) throws NoPermissionException {
		Classes classes = this.schoolRepository.findClassesById(studentInfo.getClasses());
		Faculty faculty = classes.getFaculty();
		School school = faculty.getSchool();
		
		//如果该老师修改的学生不是他学院的则不添加
		if (!school.getId().equals(this.getSchool().getId())) {
			throw new NoPermissionException("学生与老师学院不一致");
		}
		Student student = new Student();
		Account account = new Account(Role.STUDENT);
		Dormitory dormitory = this.dormitoryRepository.findById(studentInfo.getDormitory());
		
		try {
			BeanUtils.copyProperties(account, studentInfo);
		} catch (IllegalAccessException e) {
			
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			
			e.printStackTrace();
		}
		
		Date date = new Date();
	
		account.setModifyDate(date);
		account.setId(studentInfo.getId());
		
		student.setClasses(classes);
		student.setDormitory(dormitory);
		student.setFaculty(faculty);
		student.setGender('M');
		student.setSchool(school);
		student.setStudentNumber(studentInfo.getStudentNumber());
		
		student.setId(studentInfo.getId());
		student.setAccount(account);
		
		return this.studentRepository.updateStudent(student);
	}
	
	public void deleteStudentById(Long id) throws NoPermissionException {
		Student student  =this.studentRepository.findById(id);
		
		//如果该学生是这个学院，老师有权删除
		if (student.getSchool().getId().equals(this.getSchool().getId())) {
			this.studentRepository.deleteById(id);
		} else {
			throw new NoPermissionException("学生与老师学院不一致");
		}
		
	}
	
	public void processDormitoryChange(DormitoryChange dormitoryChange) throws Exception {
		
		//这里的问题是懒加载时动态代理只支持getClass和getId,equals hashCode不支持
		if (!dormitoryChange.getStudent().getSchool().getId().equals(this.getSchool().getId())) {
			throw new NoPermissionException("学生与老师学院不一致");
		}
		dormitoryChange.process();
		this.dormitoryChangeRepository.updateDormitoryChange(dormitoryChange);
	}
	
	public void applyDormitoryChange(DormitoryChange dormitoryChange) throws Exception {
		
		if (!dormitoryChange.getStudent().getSchool().getId().equals(this.getSchool().getId())) {
			throw new NoPermissionException("学生与老师学院不一致");
		}
		dormitoryChange.apply();
		this.dormitoryChangeRepository.updateDormitoryChange(dormitoryChange);
	}
	
	public void changeStudentPassword(Student student, String newPassword) throws NoPermissionException {
		if (student.getSchool().getId().equals(this.getSchool().getId())) {
			student.getAccount().setPassword(newPassword);
			this.studentRepository.updateStudent(student);
		} else {
			throw new NoPermissionException("学生与老师学院不一致");
		}
			
	}
	
	public void changePassword(String oldPassword, String newPassword) throws NoPermissionException {
		if (oldPassword.equals(this.getAccount().getPassword())) {
			this.getAccount().setPassword(newPassword);
			this.systemAdministratorRepository.updateStduentAffairTeacher(this);
		} else {
			throw new NoPermissionException("原始密码错误，无权修改密码");
		}
	}
	
	public void modifyPersonalInfo(StudentAffairTeacherDTO studentAffairTeacherInfo) throws NoPermissionException {
		
		if (!studentAffairTeacherInfo.getId().equals(this.getId())) {
			throw new NoPermissionException("你无权修改这个用户");
		}
		
		if (studentAffairTeacherInfo.getOldEmail() != null && studentAffairTeacherInfo.getEmail() != null) {
			if (studentAffairTeacherInfo.getOldEmail().equals(this.getAccount().getEmail())) {
				this.getAccount().setEmail(studentAffairTeacherInfo.getEmail());
			}
		}
		
		if (studentAffairTeacherInfo.getOldPassword() != null && studentAffairTeacherInfo.getPassword() != null) {
			if (studentAffairTeacherInfo.getOldPassword().equals(this.getAccount().getPassword())) {
				this.getAccount().setPassword(studentAffairTeacherInfo.getPassword());
			}
		}
		//确保account 和studentAffairTeacher 有一样的 Id 才能正确修改 一对一主键映射
		
		
		//记住要修改modifyDate
		account.setModifyDate(new Date());
		this.setAccount(account);
		
		this.systemAdministratorRepository.updateStduentAffairTeacher(this);
	}*/
	
	/*******************************************************************************************/
	// getter setter method 
	
/*	*//**
	 * @return the account
	 *//*
	public Account getAccount() {
		return account;
	}

	*//**
	 * @param account the account to set
	 *//*
	public void setAccount(Account account) {
		this.account = account;
	}

	*//**
	 * @return the id
	 *//*
	public Long getId() {
		return id;
	}

	*//**
	 * @param id the id to set
	 *//*
	public void setId(Long id) {
		this.id = id;
	}

	*//**
	 * @return the school
	 *//*
	public School getSchool() {
		return school;
	}

	*//**
	 * @param school the school to set
	 *//*
	public void setSchool(School school) {
		this.school = school;
	}

	*//**
	 * @return the studentRepository
	 *//*
	public StudentRepository getStudentRepository() {
		return studentRepository;
	}

	*//**
	 * @param studentRepository the studentRepository to set
	 *//*
	public void setStudentRepository(StudentRepository studentRepository) {
		this.studentRepository = studentRepository;
	}

	*//**
	 * @return the dormitoryRepository
	 *//*
	public DormitoryRepository getDormitoryRepository() {
		return dormitoryRepository;
	}

	*//**
	 * @param dormitoryRepository the dormitoryRepository to set
	 *//*
	public void setDormitoryRepository(DormitoryRepository dormitoryRepository) {
		this.dormitoryRepository = dormitoryRepository;
	}

	*//**
	 * @return the sytemAdministratorRepository
	 *//*
	public SystemAdministratorRepository getSytemAdministratorRepository() {
		return systemAdministratorRepository;
	}

	*//**
	 * @param sytemAdministratorRepository the sytemAdministratorRepository to set
	 *//*
	public void setSytemAdministratorRepository(
			SystemAdministratorRepository sytemAdministratorRepository) {
		this.systemAdministratorRepository = sytemAdministratorRepository;
	}

	*//**
	 * 
	 *//*
	public StudentAffairTeacher() {
		super();
	}

	*//**
	 * @return the schoolRepository
	 *//*
	public SchoolRepository getSchoolRepository() {
		return schoolRepository;
	}

	*//**
	 * @param schoolRepository the schoolRepository to set
	 *//*
	public void setSchoolRepository(SchoolRepository schoolRepository) {
		this.schoolRepository = schoolRepository;
	}

	 (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	 (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		StudentAffairTeacher other = (StudentAffairTeacher) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}

	*//**
	 * @return the systemAdministratorRepository
	 *//*
	public SystemAdministratorRepository getSystemAdministratorRepository() {
		return systemAdministratorRepository;
	}

	*//**
	 * @param systemAdministratorRepository the systemAdministratorRepository to set
	 *//*
	public void setSystemAdministratorRepository(
			SystemAdministratorRepository systemAdministratorRepository) {
		this.systemAdministratorRepository = systemAdministratorRepository;
	}

	*//**
	 * @return the dormitoryChangeRepository
	 *//*
	public DormitoryChangeRepository getDormitoryChangeRepository() {
		return dormitoryChangeRepository;
	}

	*//**
	 * @param dormitoryChangeRepository the dormitoryChangeRepository to set
	 *//*
	public void setDormitoryChangeRepository(
			DormitoryChangeRepository dormitoryChangeRepository) {
		this.dormitoryChangeRepository = dormitoryChangeRepository;
	}

	*//**
	 * @param studentRepository
	 * @param dormitoryRepository
	 * @param systemAdministratorRepository
	 * @param schoolRepository
	 * @param dormitoryChangeRepository
	 *//*
	public StudentAffairTeacher(StudentRepository studentRepository,
			DormitoryRepository dormitoryRepository,
			SystemAdministratorRepository systemAdministratorRepository,
			SchoolRepository schoolRepository,
			DormitoryChangeRepository dormitoryChangeRepository) {
		super();
		this.studentRepository = studentRepository;
		this.dormitoryRepository = dormitoryRepository;
		this.systemAdministratorRepository = systemAdministratorRepository;
		this.schoolRepository = schoolRepository;
		this.dormitoryChangeRepository = dormitoryChangeRepository;
	}
	

	
*/
	
}
